package com.leo.train.member.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leo.train.common.req.MemberTicketReq;
import com.leo.train.common.resp.PageResult;
import com.leo.train.common.util.SnowUtil;
import com.leo.train.member.domain.Ticket;
import com.leo.train.member.dto.TicketDto;
import com.leo.train.member.mapper.TicketMapper;
import com.leo.train.member.service.TicketService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TicketServiceImpl implements TicketService {
    @Autowired
    private TicketMapper ticketMapper;
    @Override
    public PageResult list(TicketDto ticketDto) {
        PageHelper.startPage(ticketDto.getPage(), ticketDto.getPageSize());
        Page<Ticket> page = ticketMapper.selectList(ticketDto);
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    @Override
    public void save(MemberTicketReq memberTicketReq) {
        Date now = new Date();
        Ticket ticket = new Ticket();
        BeanUtils.copyProperties(memberTicketReq, ticket);
        ticket.setId(SnowUtil.getSnowflakeNextId());
        ticket.setCreateTime(now);
        ticket.setUpdateTime(now);
        ticketMapper.insert(ticket);
    }
}
